﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Security.Permissions;
using PSIEpics;
using epics_server_remoting_class;



namespace epics_server_remoting_server
{
    class Program
    {
       
        [SecurityPermission(SecurityAction.Demand)]

        
        static void Main(string[] args)
        {
            // 创建一个IPC信道
            IpcChannel serverChannel = new IpcChannel("TestChannel");

            // 注册这个IPC信道.
            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(serverChannel);

            // 打印这个信道的名称.
            Console.WriteLine("The name of the channel is {0}.",
                serverChannel.ChannelName);

            // 打印这个信道的优先级.
            Console.WriteLine("The priority of the channel is {0}.",
                serverChannel.ChannelPriority);

            // 打印这个信道的URI数组.
            System.Runtime.Remoting.Channels.ChannelDataStore channelData = (System.Runtime.Remoting.Channels.ChannelDataStore)serverChannel.ChannelData;
            foreach (string uri in channelData.ChannelUris)
            {
                Console.WriteLine("The channel URI is {0}.", uri);
            }

            // 向信道暴露一个远程对象.
            System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(EpicsServer_Remoting), "EpicsServer_Remoting.rem", System.Runtime.Remoting.WellKnownObjectMode.Singleton);

            


            Console.WriteLine("The Server is running");
            string cmd;
            while (true)
            {
                cmd = Console.ReadLine();
                if (cmd == "quit")
                    break;
                //else
                //    RunCMD(cmd);
            }

            Console.WriteLine("The server is exiting.");


        }

        //static void RunCMD(string cmd)
        //{
        //    EpicsServer_Remoting MainObject = new EpicsServer_Remoting();
        //    string[] cmds;
        //    cmds = cmd.Split(' ');
        //    switch (cmds[0])
        //    {
        //        case "showpv":  //show all the pvs on this server
        //            { 
        //                Console.WriteLine("Record:");
        //                foreach(string pvname in MainObject.RecordList.Keys)
        //                {
        //                    Console.WriteLine(pvname);
        //                }
        //                Console.WriteLine("Array Record:");
        //                foreach(string pvname in MainObject.RecordArrayList.Keys)
        //                {
        //                    Console.WriteLine(pvname);
        //                }
        //                break;
        //            }
        //        default:
        //            {
        //                Console.WriteLine("Na Ni??");
        //                break;
        //            }
        //    }
        //}
    }
}
